package jdbc;
/**
 * 使用预编译SQL执行DML操作--INSERT
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCDemo9 {
    public static void main(String[] args) {
        UserInfo userinfo=InputUtil.getInputObject(new UserInfo(),"欢迎注册！");
        /*
        INSERT INTO userinfo(username,password,nickname,age)
        VALUES(?,?,?,?);
         */
        try (Connection conn= DBUtil.getConnection();)
        {
            String s="INSERT INTO userinfo(username,password,nickname,age)\n" +
                    "        VALUES(?,?,?,?);";
            PreparedStatement p=conn.prepareStatement(s);
            p.setString(1, userinfo.getUsername());
            p.setString(2, userinfo.getPassword());
            p.setString(3, userinfo.getNickname());
            p.setInt(4,userinfo.getAge());
            int a= p.executeUpdate();
            System.out.println(a>0?"注册成功":"注册失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
